const { ccclass, property } = cc._decorator;

@ccclass
export default class placeItem extends cc.Component {
    @property(cc.Integer)
    tag: number = 0;
    downNode: cc.Node = null;
    upNode: cc.Node = null;
    otherTag: number = 0;
    isTagStep: boolean = false;
    onLoad() {
        this.downNode = this.node.getChildByName("down");
        this.upNode = this.node.getChildByName("up");
        this.node.on(cc.Node.EventType.TOUCH_START, this.emitEvents, this);
    }
    init() {
        if (!this.downNode)
            this.downNode = this.node.getChildByName("down");
        if (!this.upNode)
            this.upNode = this.node.getChildByName("up");
        this.upNode.active = true;
        this.downNode.active = true;
        this.otherTag = 0;
        this.isTagStep = false;
    }
    private emitEvents() {
        let event = new cc.Event.EventCustom('onNodeClick', true);
        event.setUserData({
            target: this,
            tag: this.tag,
            position: this.node.position
        });
        this.node.dispatchEvent(event);
    }
    onCollisionEnter(other, self) {
        if (other.tag == TagHeroBody || other.tag == TagBox) {
            this.downNode.active = false;
            this.upNode.active = false;
            this.otherTag++;
        }
        else if (other.tag == TagStep) {
            this.downNode.active = false;
            this.upNode.active = false;
            this.isTagStep = true;
        }
    }
    onCollisionExit(other, self) {
        if (!this.isTagStep && (other.tag == TagHeroBody || other.tag == TagBox)) {
            this.otherTag--;
            if (!this.downNode.active && this.otherTag == 0) {
                this.downNode.active = true;
                this.upNode.active = true;
            }
        }
    }
}
